package cn.edu.cuc.yjsy.admin.util;

import java.util.Locale;

public class Op {
	public static String CHNEncode = "GBK";
	public static String ENGEncode = "ISO-8859-1";
	public static String nvl(String s) {
		if (s==null)
		  return "";
		else
		  return s;
	}

	public static String nvl(String s, String val) {
		if (s==null)
		  return val;
		else
		  return s;
	}
	
	private static String codeString(String str, String resCodeString, String desCodeString){
		String v = null;
		str = nvl(str, "");
		try {
			if(resCodeString == desCodeString){
				v = str;
			}else{
//				byte[] temp = str.getBytes(resCodeString);
//				if(java.util.Arrays.equals(str.getBytes(), temp))
//				if (str.equals(new String(temp)))
				v = new String(str.getBytes(resCodeString), desCodeString);
//				else
//					v = str;				
			}
		} catch(Exception e) {
			e.printStackTrace();
		}
		return v;
	}
	
	public static String getLocaleCode(){
//		System.out.println(System.getProperty("file.encoding"));
		if(Locale.getDefault().getLanguage().equals("zh"))
			return Op.CHNEncode;
		else
			return Op.ENGEncode;
	}
	
	public static String toGBKString(String str){
		return codeString(str, getLocaleCode(), Op.CHNEncode);
	}
	
	public static String toLocaleString(String str){
		return codeString(str, Op.CHNEncode, getLocaleCode());
	}
}
